package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserAccountDetailMapper userAccountDetailMapper;

    @Override
    public Map<String, Object> submitRecharge(RechargeInfoVo rechargeInfoVo) {
        Map<String, Object> map = new HashMap<>();
        RechargeInfo rechargeInfo = new RechargeInfo();
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
        rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
        rechargeInfo.setPayWay(rechargeInfo.getPayWay());
        rechargeInfo.setUserId(AuthContextHolder.getUserId());
        rechargeInfo.setOrderNo(IdWorker.getIdStr());
        this.save(rechargeInfo);
        map.put("orderNo", rechargeInfo.getOrderNo());
        return map;
    }

    @Override
    public void updateRechargeInfo(String orderNo) {
        RechargeInfo rechargeInfo = this.rechargeInfoMapper.selectOne(new LambdaQueryWrapper<RechargeInfo>()
                .eq(RechargeInfo::getOrderNo, orderNo));
        if(rechargeInfo == null){
            return;
        }
        //更新充值记录表
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
        boolean flag = this.update(new LambdaQueryWrapper<RechargeInfo>().eq(RechargeInfo::getOrderNo, orderNo));
        if (flag) {
            //账户表更新money
            this.userAccountMapper.insertMoney(rechargeInfo.getRechargeAmount(), rechargeInfo.getUserId());
            //记录账户变化明细
            UserAccountDetail accountDetail = new UserAccountDetail();
            accountDetail.setTitle("账户充值");
            accountDetail.setTradeType(SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT);
            accountDetail.setOrderNo(orderNo);
            accountDetail.setAmount(rechargeInfo.getRechargeAmount());
            accountDetail.setUserId(rechargeInfo.getUserId());
            this.userAccountDetailMapper.insert(accountDetail);
        }
    }
}
